% IndividualCellTracking.m
% Eden Ford
% 10 December 2023

% Code to input, process, and visualize TrackMate data

% General definitions:
% Object - cell or cluster of cells that are touching/interacting with one
% another

close all
clear all
clc

FrameCutOff = 5; % cell objects tracked for 5 frames or less will not be included

%% Import 0 mM CMP, Remove Background
BaseTrack = [1 4 12 20 7 3]; % track number determined to be stationary
LocationTrack0 = [];
for i = 1:6 % Do this for each image set
    [num,txt,raw] = xlsread('0mMCMP_Spots_MATLAB.xlsx',i); % import sheet
    LocationTrackNew = [num(:,2) num(:,5) num(:,3) num(:,4)]; % organize data: TrackID, Time(hr), X(um), Y(um)
    LocationTrackNew = sortrows(LocationTrackNew, [1 2]); % sort by trackID then time
    
    % determine gel background movement
    NoiseTrack = [];
    for j = 1:length(LocationTrackNew)
        if LocationTrackNew(j,1) == BaseTrack(i)
            NoiseTrack = [NoiseTrack; LocationTrackNew(j,:)];
        end
    end
    
    % subtract gel background movement
    LocationTrackNew_BG = LocationTrackNew;
    for j = 1:length(LocationTrackNew)
        for k = 1:length(NoiseTrack)
            if LocationTrackNew(j,2) == NoiseTrack(k,2)
                LocationTrackNew_BG(j,3) = LocationTrackNew(j,3) - NoiseTrack(k,3);
                LocationTrackNew_BG(j,4) = LocationTrackNew(j,4) - NoiseTrack(k,4);
            end
        end
    end
        
    % remove stationary points
    LocationTrackNew_BGremoved = [];
    for j = 1:length(LocationTrackNew_BG)
        if LocationTrackNew_BG(j,1) == BaseTrack(i)
            continue
        else
            LocationTrackNew_BGremoved = [LocationTrackNew_BGremoved; LocationTrackNew_BG(j,:)];
        end
    end
    
    % remove points below the frame cutoff
    TracksToExclude = [];
    First = LocationTrackNew_BGremoved(1,2);
    for j = 2:length(LocationTrackNew_BGremoved)
        if LocationTrackNew_BGremoved(j,1) == LocationTrackNew_BGremoved(j-1,1)
            if j == length(LocationTrackNew_BGremoved)
                Final = LocationTrackNew_BGremoved(j,2);
                TotalTime = Final - First;
                if TotalTime <= FrameCutOff/2
                    TracksToExclude = [TracksToExclude; LocationTrackNew_BGremoved(j,1)];
                end
            else
                continue
            end
        else
            Final = LocationTrackNew_BGremoved(j-1,2);
            TotalTime = Final - First;
            First = LocationTrackNew_BGremoved(j,2);
            if TotalTime <= FrameCutOff/2
                TracksToExclude = [TracksToExclude; LocationTrackNew_BGremoved(j-1,1)];
            end
        end
    end
    LocationTrackClean = [];
    Test = 0;
    for j = 1:length(LocationTrackNew_BGremoved)
        for k = 1:length(TracksToExclude)
            if LocationTrackNew_BGremoved(j,1) == TracksToExclude(k)
                Test = Test + 1;
            else
                Test = Test + 0;
            end
        end
        if Test == 0
            LocationTrackClean = [LocationTrackClean; LocationTrackNew_BGremoved(j,:)];
        end
        Test = 0;
    end
    LocationTrack0 = [LocationTrack0; LocationTrackClean];
end
%LocationTrack0

%% Import 10 mM CMP, Remove Background
BaseTrack = [43 0 2 9 63 20]; % track number determined to be stationary
LocationTrack10 = [];
for i = 1:6 % Do this for each image set
    [num,txt,raw] = xlsread('10mMCMP_Spots_MATLAB.xlsx',i); % import sheet
    LocationTrackNew = [num(:,2) num(:,5) num(:,3) num(:,4)]; % organize data: TrackID, Time(hr), X(um), Y(um)
    LocationTrackNew = sortrows(LocationTrackNew, [1 2]); % sort by trackID then time
    
    % determine gel background movement
    NoiseTrack = [];
    for j = 1:length(LocationTrackNew)
        if LocationTrackNew(j,1) == BaseTrack(i)
            NoiseTrack = [NoiseTrack; LocationTrackNew(j,:)];
        end
    end
    
    % subtract gel background movement
    LocationTrackNew_BG = LocationTrackNew;
    for j = 1:length(LocationTrackNew)
        for k = 1:length(NoiseTrack)
            if LocationTrackNew(j,2) == NoiseTrack(k,2)
                LocationTrackNew_BG(j,3) = LocationTrackNew(j,3) - NoiseTrack(k,3);
                LocationTrackNew_BG(j,4) = LocationTrackNew(j,4) - NoiseTrack(k,4);
            end
        end
    end
        
    % remove stationary points
    LocationTrackNew_BGremoved = [];
    for j = 1:length(LocationTrackNew_BG)
        if LocationTrackNew_BG(j,1) == BaseTrack(i)
            continue
        else
            LocationTrackNew_BGremoved = [LocationTrackNew_BGremoved; LocationTrackNew_BG(j,:)];
        end
    end
    
    % remove points below the frame cutoff
    TracksToExclude = [];
    First = LocationTrackNew_BGremoved(1,2);
    for j = 2:length(LocationTrackNew_BGremoved)
        if LocationTrackNew_BGremoved(j,1) == LocationTrackNew_BGremoved(j-1,1)
            if j == length(LocationTrackNew_BGremoved)
                Final = LocationTrackNew_BGremoved(j,2);
                TotalTime = Final - First;
                if TotalTime <= FrameCutOff/2
                    TracksToExclude = [TracksToExclude; LocationTrackNew_BGremoved(j,1)];
                end
            else
                continue
            end
        else
            Final = LocationTrackNew_BGremoved(j-1,2);
            TotalTime = Final - First;
            First = LocationTrackNew_BGremoved(j,2);
            if TotalTime <= FrameCutOff/2
                TracksToExclude = [TracksToExclude; LocationTrackNew_BGremoved(j-1,1)];
            end
        end
    end
    LocationTrackClean = [];
    Test = 0;
    for j = 1:length(LocationTrackNew_BGremoved)
        for k = 1:length(TracksToExclude)
            if LocationTrackNew_BGremoved(j,1) == TracksToExclude(k)
                Test = Test + 1;
            else
                Test = Test + 0;
            end
        end
        if Test == 0
            LocationTrackClean = [LocationTrackClean; LocationTrackNew_BGremoved(j,:)];
        end
        Test = 0;
    end
    LocationTrack10 = [LocationTrack10; LocationTrackClean];
end
%LocationTrack10

%% Set first point as origin
% 0 mM mfCMP
LocationTrack0_Norm = [LocationTrack0(1,1:2), 0.0, 0.0];
OriginX = LocationTrack0(1,3);
OriginY = LocationTrack0(1,4);
for i = 2:length(LocationTrack0)
    if LocationTrack0(i,1) == LocationTrack0(i-1,1) % then it's the same track
        X = LocationTrack0(i,3) - OriginX;
        Y = LocationTrack0(i,4) - OriginY;
        LocationTrack0_Norm = [LocationTrack0_Norm; LocationTrack0(i,1:2), X, Y];
    else %it's a new track
        LocationTrack0_Norm = [LocationTrack0_Norm; LocationTrack0(i,1:2), 0.0, 0.0];
        OriginX = LocationTrack0(i,3);
        OriginY = LocationTrack0(i,4);
    end
end

% 10 mM mfCMP
LocationTrack10_Norm = [LocationTrack10(1,1:2), 0.0, 0.0];
OriginX = LocationTrack10(1,3);
OriginY = LocationTrack10(1,4);
for i = 2:length(LocationTrack10)
    if LocationTrack10(i,1) == LocationTrack10(i-1,1) % then it's the same track
        X = LocationTrack10(i,3) - OriginX;
        Y = LocationTrack10(i,4) - OriginY;
        LocationTrack10_Norm = [LocationTrack10_Norm; LocationTrack10(i,1:2), X, Y];
    else %it's a new track
        LocationTrack10_Norm = [LocationTrack10_Norm; LocationTrack10(i,1:2), 0.0, 0.0];
        OriginX = LocationTrack10(i,3);
        OriginY = LocationTrack10(i,4);
    end
end

%% Graph cell tracks data
% graph bounds
xMin = -200;
xMax = 200;
yMin = -200;
yMax = 200;

% 0 mM mfCMP
figure
title('0 mM mfCMP')
hold on
x = [0];
y = [0];
for i = 2:length(LocationTrack0_Norm)
    if LocationTrack0_Norm(i,1) == LocationTrack0_Norm(i-1,1) % then it's the same track
        x = [x, LocationTrack0_Norm(i,3)];
        y = [y, LocationTrack0_Norm(i,4)];
    else %it's a new track
        % add to plot first
        plot(x,y)
        % now reset vectors
        x = [0];
        y = [0];
    end
end
xlim([xMin xMax])
ylim([yMin yMax])
xlabel('distance x (\mum)')
ylabel('distance y (\mum)')
hold off

% 10 mM mfCMP
figure
title('10 mM mfCMP')
hold on
x = [0];
y = [0];
for i = 2:length(LocationTrack10_Norm)
    if LocationTrack10_Norm(i,1) == LocationTrack10_Norm(i-1,1) % then it's the same track
        x = [x, LocationTrack10_Norm(i,3)];
        y = [y, LocationTrack10_Norm(i,4)];
    else %it's a new track
        % add to plot first
        plot(x,y)
        % now reset vectors
        x = [0];
        y = [0];
    end
end
xlim([xMin xMax])
ylim([yMin yMax])
xlabel('distance x (\mum)')
ylabel('distance y (\mum)')
hold off